/*
toastr.info('这是一条消息，将在5秒后自动消失。', '提示', {
    timeOut: 5000, // 5 秒后自动关闭
    closeButton: false, // 不显示关闭按钮
    progressBar: true // 显示进度条
});
*/

async function pasteFromClipboardSync() {    
    if (navigator.clipboard) {
        try {        
            const pastedText = await navigator.clipboard.readText();
            return pastedText;
        } catch (err) {
            toastr.warning(err, '无法粘贴文本');
        }
    } else {
        const inputNode = document.createElement('input')
        document.body.appendChild(inputNode)
        inputNode.focus();
        document.execCommand('paste');
        const pastedText = inputNode.value;
        document.body.removeChild(inputNode);
        return pastedText;        
    }
}

async function copyToClipboardSync(str) {
    try {
        await navigator.clipboard.writeText(str);
        toastr.success('复制成功~');
    } catch (err) {
        toastr.warning(err, '无法复制文本');
    }
}

function copyToClipboard(str) {    
    if (location.protocol === 'https:') {
        navigator.clipboard.writeText(str).then(() => {
            toastr.success("复制成功~")
        }).catch((err) => {
            toastr.warning(err, "复制失败，请手动复制(╥﹏╥)")
        })
    } else {
        const inputNode = document.createElement('input')
        inputNode.value = str
        document.body.appendChild(inputNode)
        inputNode.select()
        document.execCommand('copy')
        document.body.removeChild(inputNode)
        toastr.success("复制成功~")
    }
}

var loadFiles = {
    js     : [],
    css    : [],
    plugin : []
};


function loadCSS(fileName, callback, into) {
    into       = into     || "head";        
    callback   = callback || function() {};
    
    var css    = document.createElement("link");
    css.type   = "text/css";
    css.rel    = "stylesheet";
    css.onload = css.onreadystatechange = function() {
        loadFiles.css.push(fileName);
        callback();
    };

    css.href = fileName;
    if (!fileName.endsWith(".css"))
        css.href += ".css";

    if(into === "head") {
        document.getElementsByTagName("head")[0].appendChild(css);
    } else {
        document.body.appendChild(css);
    }
};
    
    
var isIE    = (navigator.appName == "Microsoft Internet Explorer");
var isIE8   = (isIE && navigator.appVersion.match(/8./i) == "8.");

function loadScript(fileName, callback, into) {

    into          = into     || "head";
    callback      = callback || function() {};

    var script    = null; 
    script        = document.createElement("script");
    script.id     = fileName.replace(/[\./]+/g, "-");
    script.type   = "text/javascript";     
    script.src    = fileName;
    if (!fileName.endsWith(".js"))
        css.href += ".js";
    
    if (isIE8) 
    {            
        script.onreadystatechange = function() {
            if(script.readyState) 
            {
                if (script.readyState === "loaded" || script.readyState === "complete") 
                {
                    script.onreadystatechange = null; 
                    loadFiles.js.push(fileName);
                    callback();
                }
            } 
        };
    }
    else
    {
        script.onload = function() {
            loadFiles.js.push(fileName);
            callback();
        };
    }

    if (into === "head") {
        document.getElementsByTagName("head")[0].appendChild(script);
    } else {
        document.body.appendChild(script);
    }
};

function detectDeviceAndOS() {
    const userAgent = navigator.userAgent;

    // 判断设备类型
    let deviceType;
    if (/Mobi|Android/i.test(userAgent)) {
        deviceType = '手机';
    } else {
        deviceType = 'PC';
    }

    // 判断操作系统
    let osType;
    if (/iPhone|iPad|iPod/i.test(userAgent)) {
        osType = 'iOS';
    } else if (/Android/i.test(userAgent)) {
        osType = 'Android';
    } else {
        osType = '其他';
    }

    return { deviceType, osType };
}
